<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<center>
		<a href="index.php">首页</a>
		<?php
		if (!empty($_COOKIE['username'])) {
			echo '欢迎你,'.$_COOKIE['username'].'<a href="logout.php">退出</a>';
		} else {
		?>
		<a href="login.html">登录</a>
		<a href="reg.html">注册</a>
		<?php } ?>
		<hr>
		<table border="1">
			<tr>
				<td>编号</td>
				<td>用户名</td>
				<td>年龄</td>
				<td>密码</td>
				<td>操作</td>
			</tr>
			<?php
			// 引入配置文件
			require('include.php');

			// 获取数据，遍历数据库
			$ids = $redis->get('user:id');

			// 分页需要获取的数据：总条数 当前页 每页数据条数 －>计算出多少页
			$total = $redis->lsize('user:list'); // 总条数
			$page = (!empty($_GET['p']) ? $_GET['p'] : 1);	//当前页
			$every = 5;		// 每页数据条数
			$pages = ceil($total/$every);	//总页码

			// lrange key 0 4		1
			// lrange key 5 9		2
			// lrange key 10 14		3
			$list = $redis->lrange('user:list', ($page-1)*$every, $page*$every-1);

			foreach ($list as $k => $v) {
				$data = $redis->hgetall("user:{$v}");

			// 使用for循环遍历数据
			// for ($i=1; $i<=$ids; $i++) {
			// 	$data = $redis->hgetall("user:{$i}");
			// 	if ($data) {
			?>
			<tr>
				<td><?php echo $data['id']; ?></td>
				<td><?php echo $data['username']; ?></td>
				<td><?php echo $data['age']; ?></td>
				<td><?php echo $data['password']; ?></td>
				<td>
					<a href="edit.php?id=<?php echo $data['id']; ?>">修改</a>
					<a href="del.php?id=<?php echo $data['id']; ?>">删除</a>
					<a href="adds.php?id=<?php echo $data['id']; ?>&uid=<?php echo $_COOKIE['id'];?>">添加关注</a>
				</td>
			</tr>
			<?php
				}
				if ($page<=1) {
					$pre = 1;
				} else {
					$pre = $page-1;
				}

				if ($page>=$pages) {
					$next = $pages;
				} else {
					$next = $page+1;
				}
			?>
			<tr>
				<td colspan="5">
					<a href="?p=<?php echo $pre; ?>">上一页</a>
					<a href="?p=<?php echo $next; ?>">下一页</a>
					一共<?php echo $total; ?> 条数据
					一共<?php echo $pages; ?> 页
					当前第<?php echo $page; ?>页

				</td>
			</tr>
		</table>
		<table>
			<tr>
				<td>我关注了谁</td>
			</tr>
			<?php
				$ids = $redis->smembers("user:{$_COOKIE['id']}:guanzhu");
				foreach ($ids as $id) {
					$data = $redis->hgetall("user:{$id}");

				?>
				<tr>
					<td><?php echo $data['username']; ?></td>
				</tr>
				<?php
				}
			?>
			<tr>
				<td>我被谁关注</td>
			</tr>
			<?php
				$ids = $redis->smembers("user:{$_COOKIE['id']}:beiguanzhu");
				foreach ($ids as $id) {
					$data = $redis->hgetall("user:{$id}");
			?>
			<tr>
				<td><?php echo $data['username']; ?></td>
			</tr>
			<?php
				}
			?>
		</table>
	</center>
</body>
</html>